<?php

namespace app\common\service;

use fast\Http;

/**
 * 数脉API服务类
 */
class ShumaiApiService
{

    /**
     * 接口网关
     * @var string
     */
    protected $gatewayUrl = 'https://smqysys.market.alicloudapi.com';

    /**
     * 配置参数
     * @var string
     */
    protected $appcode = '';

    /**
     * 服务初始化
     */
    public function __construct()
    {
        $this->appcode = config('shumai.appcode');
    }

    /**
     * 企业四要素校验
     * @param string $companyName 公司名称
     * @param string $creditNo 	  统一信用代码
     * @param string $legalPerson 法人姓名
     * @param string $idCard 	  法人身份证号
     * @return array
     */
    public function companyFourElementCheck(string $companyName = '', string $creditNo = '', string $legalPerson = '', string $idCard = '') :array
    {
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $this->appcode);
        $result = Http::get(
            $this->gatewayUrl . '/company4/check',
            compact('companyName', 'creditNo', 'legalPerson', 'idCard'),
            [CURLOPT_HTTPHEADER => $headers]
        );
        $result = json_decode($result, true);
        if (isset($result['code']) && 200 !== $result['code']) {
            return ['code' => 0, 'msg' => '服务错误：' . $result['msg'] . ' 错误码：' . $result['code'], 'data' => []];
        }
        return ['code' => 1, 'msg' => 'success', 'data' => $result['data']];
    }

    /**
     * 企业三要素校验
     * @param string $companyName 公司名称
     * @param string $creditNo 	  统一信用代码
     * @param string $legalPerson 法人姓名
     * @return array
     */
    public function companyThreeElementCheck(string $companyName = '', string $creditNo = '', string $legalPerson = '') :array
    {
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $this->appcode);
        $result = Http::get(
            'https://smkjqysys.market.alicloudapi.com/company-three/check/v2',
            compact('companyName', 'creditNo', 'legalPerson'),
            [CURLOPT_HTTPHEADER => $headers]
        );
        $result = json_decode($result, true);
        if (isset($result['code']) && 200 !== $result['code']) {
            return ['code' => 0, 'msg' => '服务错误：' . $result['msg'] . ' 错误码：' . $result['code'], 'data' => []];
        }
        return ['code' => 1, 'msg' => 'success', 'data' => $result['data']];
    }
}
